//
//  JEHSearchWebViewController.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/23.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHSearchWebViewController.h"

@interface JEHSearchWebViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) JEHDataBaseHandle *handle;

@end

@implementation JEHSearchWebViewController

- (void)dealloc {
    
    [_searchModel release];
    [_webView release];
    [_handle release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self createWebView];
    [self createHeaderView];
}

- (void)setSearchModel:(JEHSearchModel *)searchModel {
    if (_searchModel != searchModel) {
        [_searchModel release];
        _searchModel = [searchModel retain];
    }
}

#pragma mark - passLink
- (void)createWebView {
    
    self.webView = [[UIWebView alloc] init];
    [_webView release];
    _webView.scrollView.bounces = NO;
    _webView.frame = CGRectMake(0, 50, self.view.width, self.view.height - 50);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.searchModel.url]];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
}

#pragma mark - createHeaderView
- (void)createHeaderView {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 50)];
    headerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:headerView];
    
    UIButton *goBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [goBack setImage:[UIImage imageNamed:@"goBackButton"] forState:UIControlStateNormal];
    goBack.frame = CGRectMake(10, 20, 60, kHeaderHeight);
    [goBack addTarget:self action:@selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:goBack];
    
    UIButton *mood = [UIButton buttonWithType:UIButtonTypeCustom];
    [mood setImage:[UIImage imageNamed:@"un_selectMood"] forState:UIControlStateNormal];
    mood.frame = CGRectMake(self.view.width - 190, 20, 60, kHeaderHeight);
    [mood addTarget:self action:@selector(moodAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:mood];
    
    UIButton *collect = [UIButton buttonWithType:UIButtonTypeCustom];
    collect.frame = CGRectMake(self.view.width - 130, 20, 60, kHeaderHeight);
    [collect addTarget:self action:@selector(collectedAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:collect];
    
    self.handle = [JEHDataBaseHandle shareDataBaseHandle];
    [self.handle openDataBase];
    JEHCollectionModel *collectionModel = [self.handle selectWithTitle:self.searchModel.screen_name];
    [self.handle closeDataBase];
    
    if (![collectionModel.title isEqualToString:_searchModel.screen_name]) {
        [collect setImage:[UIImage imageNamed:@"un_selectCollected"] forState:UIControlStateNormal];
    } else {
        [collect setImage:[UIImage imageNamed:@"collected"] forState:UIControlStateNormal];
    }
    
    UIButton *share = [UIButton buttonWithType:UIButtonTypeCustom];
    share.frame = CGRectMake(self.view.width - 70, 20, 60, kHeaderHeight);
    [share setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [share addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:share];
}

- (void)goBackAction:(UIButton *)sender {
    
    [self.webView removeFromSuperview];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

static BOOL isSelect1 = NO;

- (void)moodAction:(UIButton *)sender {
    
    if (isSelect1) {
        
        [sender setImage:[UIImage imageNamed:@"un_selectMood"] forState:UIControlStateNormal];
        isSelect1 = NO;
    } else {
        [sender setImage:[UIImage imageNamed:@"mood"] forState:UIControlStateNormal];
        isSelect1 = YES;
    }
}

- (void)collectedAction:(UIButton *)sender {
    
    if (_searchModel) {
        
        [self.handle openDataBase];
        JEHCollectionModel *collectionModel = [self.handle selectWithTitle:self.searchModel.screen_name];
        if (![collectionModel.title isEqualToString:_searchModel.screen_name]) {
            
            collectionModel.title = _searchModel.screen_name;
            collectionModel.category = @"A";
            collectionModel.link = _searchModel.url;
            [self.handle insertIntoDataBase:collectionModel];
            [self.handle closeDataBase];
            [sender setImage:[UIImage imageNamed:@"collected"] forState:UIControlStateNormal];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [alert dismissWithClickedButtonIndex:0 animated:NO];
            });
            [alert release];
        } else {
            [self.handle deleteFromDataBase:_searchModel.screen_name];
            [self.handle closeDataBase];
            [sender setImage:[UIImage imageNamed:@"un_selectCollected"] forState:UIControlStateNormal];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"取消收藏成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [alert dismissWithClickedButtonIndex:0 animated:NO];
            });
            [alert release];
        }
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"collectionReloadData" object:nil];
    }
}

#pragma mark - share
- (void)shareAction:(UIButton *)sender {
    
    if (_searchModel) {
        
        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"566fbfa9e0f55a1273002780" shareText:self.searchModel.url shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, nil] delegate:nil];
    }
}

@end
